import 'dart:ui';

import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../utils/httputils/api.dart';
import '../../utils/httputils/resultdata.dart';
import 'index.dart';
import 'models/ListCentralEntity.dart';
import 'models/ProjectStatistical.dart';

class StatisticsController extends GetxController {
  StatisticsController();

  final state = StatisticsState();

  int count1 = 0;
  int count2 = 0;

  List<Color> colors = [Colors.deepOrangeAccent,Colors.deepPurple,Colors.greenAccent,Colors.blueAccent,Colors.amberAccent];

  // tap
  void handleTap(int index) {
    Get.snackbar(
      "标题",
      "消息",
    );
  }

  /// 在 widget 内存中分配后立即调用。
  @override
  void onInit() {
    super.onInit();
    getReportDetail();
    listCentralStatisticalData();
    update();
  }

  /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  @override
  void onReady() {
    super.onReady();
  }

  /// 在 [onDelete] 方法之前调用。
  @override
  void onClose() {
    super.onClose();
  }

  /// dispose 释放内存
  @override
  void dispose() {
    super.dispose();
  }



  getReportDetail() async{
    var params = {'endTime': "", 'startTime': ""};

    ResultData response = await Api.getProjectStatisticalData(params);

    if (response.isSuccess) {
      state.projectStatistical = ProjectStatistical.fromJson(response.data);
      for(int i=0;i<state.projectStatistical.phaseCode!.length;i++){

        count1 += state.projectStatistical.phaseCode![i].cou?.toInt()??0;


        state.doughnutData!.add(BrnDoughnutDataItem(value: state.projectStatistical.phaseCode![i].cou!.toDouble(), title: state.projectStatistical.phaseCode![i].code??'',color: colors[i]));
        if(i==0){
          state.selectedItem = state.doughnutData![0];
        }

      }
      for(int i=0;i<state.projectStatistical.projectType!.length;i++){

        count2 += state.projectStatistical.projectType![i].cou?.toInt()??0;
        state.doughnutDataByType!.add(BrnDoughnutDataItem(value: state.projectStatistical.projectType![i].cou!.toDouble(), title: state.projectStatistical.projectType![i].code??'',color: colors[i]));
        if(i==0){
          state.selectedItemByType = state.doughnutDataByType![0];
        }

      }
    }
  }

  listCentralStatisticalData() async{
    var params = {'endTime': "", 'startTime': ""};

    ResultData response = await Api.listCentralStatisticalData(params);

    if (response.isSuccess) {
      List _list = response.data;

      state.listCentralEntity = _list.map((e) => ListCentralEntity.fromJson(e)).toList();

      update();
    }
  }

}
